<template>
    <div class="page-container">
        <!--工具栏-->
        <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
            <el-form :inline="true" :model="filters" :size="size">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="标题" clearable></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="exam.type" clearable placeholder="请选择类型">
                        <el-option v-for="item in exam.types"
                                   :key="item.value" :label="item.text" :value="item.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="exam.status" clearable placeholder="请选择状态">
                        <el-option v-for="item in exam.statuses"
                                   :key="item.value" :label="item.text" :value="item.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <kt-button icon="fa fa-search" :label="$t('action.search')" perms="sys:role:view" type="primary"
                               @click="findPage(null)"/>
                </el-form-item>
                <el-form-item>
                    <kt-button icon="fa fa-plus" :label="$t('action.add')" perms="sys:user:add" type="primary"
                               @click="handleAdd"/>
                </el-form-item>
            </el-form>
        </div>
        <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
            <el-form :inline="true" :size="size">
                <el-form-item>
                    <el-button-group>
                        <el-tooltip content="刷新" placement="top">
                            <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
                        </el-tooltip>
                        <el-tooltip content="列显示" placement="top">
                            <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
                        </el-tooltip>
                    </el-button-group>
                </el-form-item>
            </el-form>
            <!--表格显示列界面-->
            <table-column-filter-dialog ref="tableColumnFilterDialog" :columns="columns"
                                        @handleFilterColumns="handleFilterColumns">
            </table-column-filter-dialog>
        </div>
        <kt-table :data="pageResult" :columns="filterColumns" @findPage="findPage" @handleEdit="handleEdit"
                  @handleDelete="handleDelete">
        </kt-table>
    </div>
</template>

<script>
  import PopupTreeInput from '@/components/PopupTreeInput'
  import KtTable from '@/components/Core/KtTable'
  import KtButton from '@/components/Core/KtButton'
  import TableColumnFilterDialog from '@/components/Core/TableColumnFilterDialog'

  export default {
    components: {
      PopupTreeInput,
      KtTable,
      KtButton,
      TableColumnFilterDialog
    },
    data () {
      return {
        filters: {
          name: '',
          authorName: ''
        },
        pageRequest: {pageNum: 1, pageSize: 15},
        pageResult: {
          totalSize: 0,
          content: []
        },
        columns: [],
        filterColumns: [],
        size: 'small',
        exam: {
          type: 0,
          status: 2,
          types: [{
            value: 0,
            text: '文章'
          }, {
            value: 1,
            text: '题目'
          }, {
            value: 2,
            text: '经验交流'
          }, {
            value: 3,
            text: '解决方案'
          },{
            value: 4,
            text: '职位信息'
          }, {
            value: 5,
            text: '其他'
          }],
          statuses: [{
            value: 1,
            text: '待审核'
          }, {
            value: 2,
            text: '待提交'
          }, {
            value: 3,
            text: '已删除'
          }, {
            value: 4,
            text: '隐藏'
          }, {
            value: 5,
            text: '已审核'
          }, {
            value: 6,
            text: '已拒绝'
          }]
        }
      }
    },
    methods: {
      findPage: function (data) {
        if (data != null) {
          this.pageRequest = data.pageRequest
        }
        this.pageRequest.title = this.filters.name
        this.pageRequest.authorName = this.filters.authorName
        this.pageRequest.type = this.exam.type
        this.pageRequest.status = this.exam.status
        this.$api.article.findPage(this.pageRequest).then((res) => {
          this.pageResult.totalSize = res.body.totalSize
          this.pageResult.content = []
          res.body.content.forEach(p => {
            p.type = this.exam.types[p.type]
            p.status = this.exam.statuses[p.status - 1]
            this.pageResult.content.push(p)
          })
        }).then(data != null ? data.callback : '')
      },
      // 处理表格列过滤显示
      initColumns: function () {
        this.columns = [
          {prop: 'title', label: '文章标题', minWidth: 200},
          {prop: 'createTime', label: '创建时间', minWidth: 120},
          {prop: 'unique', label: '唯一码', minWidth: 120},
          {prop: 'type', label: '类型', width: 120},
          {prop: 'status', label: '状态', width: 120},
          {prop: 'reason', label: '审核信息', width: 180},
          // {prop:"createBy", label:"创建人", minWidth:120},
          // {prop:"createTime", label:"创建时间", minWidth:120, formatter:this.dateFormat}
          // {prop:"lastUpdateBy", label:"更新人", minWidth:100},
          // {prop:"lastUpdateTime", label:"更新时间", minWidth:120, formatter:this.dateFormat}
        ]
        this.filterColumns = JSON.parse(JSON.stringify(this.columns))
      },
      handleEdit: function (data) {
        this.$router.push({
          path: '/content/resourceEdit',
          query: {
            id: data.row.unique,
            type: data.row.type.value
          }
        })
      },
      handleAdd: function (data) {
        this.$router.push('/content/resourceEdit')
      },
      handleDelete: function (data) {
        this.$api.article.batchDelete(data.params).then((res) => {
          data.callback(res)
        })
      },
      // 处理表格列过滤显示
      handleFilterColumns: function (data) {
        this.filterColumns = data.filterColumns
        this.$refs.tableColumnFilterDialog.setDialogVisible(false)
      },
      // 处理表格列过滤显示
      displayFilterColumnsDialog: function () {
        this.$refs.tableColumnFilterDialog.setDialogVisible(true)
      },
    },
    mounted () {
      this.initColumns()
    }
  }
</script>

<style scoped>
    .page-container {
        height: 770px;
    }
</style>
